<template>
	<view class="container">
		<uni-nav-bar left-icon="left" backgroundColor="#4176ED" :border="false" color="#fff"
			@clickLeft="onClickNavBack">
			<view class="nav-title">提现</view>
		</uni-nav-bar>
		<!-- 	<view class="value-box">
			<view class="title">
				收款人姓名
			</view>
			<view class="value">

				<uni-easyinput class="input-value" v-model="valueInfo.pyeeName" style="font-size: 16px;" type="number"
					placeholder="请输入收款人姓名" placeholderStyle="font-size:13px" clearSize="18" :inputBorder="false" />

			</view>

		</view> -->
		<!-- <view class="card-box">
			<view class="title">
				收款人姓名
			</view>
			<view class="value">

				<uni-easyinput class="input-value" v-model="valueInfo.pyeeName" style="font-size: 16px;" type="number"
					placeholder="请输入收款人姓名" placeholderStyle="font-size:13px" clearSize="18" :inputBorder="false" />

			</view>
			<view class="title">
				到账银行卡
			</view>
			<view class="value">
				<view class="left">
					<image class="icon" src="../../../static/images/unionPay.png" mode=""></image>
					<view class="cardNo" v-if="cardNo">{{cardNo}}</view>
					<view class="cardNo" v-else>请选择到账银行卡</view>
				</view>
				<view class="right">
					<uni-icons type="right" size="16" @click="chooseCard"></uni-icons>

				</view>



			</view>

		</view> -->
		<view class="value-box">
			<view class="title">
				收款人姓名
			</view>
			<view class="value">

				<uni-easyinput class="input-value" v-model="valueInfo.pyeeName" style="font-size: 16px;" type="text"
					placeholder="请输入收款人姓名" placeholderStyle="font-size:13px" clearSize="18" :inputBorder="false" />

			</view>
			<view class="line"></view>
			<view class="title">
				到账银行卡
			</view>
			<view class="value">
				<view class="left" style="width: 100%;">
					<image class="icon" src="../../../static/images/unionPay.png" mode=""></image>
					<input style="width: 100%;" v-model="cardNo" placeholder="请输入银行卡号" />
					<!-- <view class="cardNo" v-if="cardNo">{{cardNo}}</view>
					<view class="cardNo" v-else>请选择到账银行卡</view> -->
				</view>
				<!-- <view class="right">
					<uni-icons type="right" size="16" @click="chooseCard"></uni-icons>
				</view> -->



			</view>
			<view class="line"></view>
			<view class="title">
				提现金额
			</view>
			<view class="value">
				<!-- <image src="../../../static/images/rmb.png" mode="aspectFit"></image> -->
				<view class="rmb">¥</view>
				<uni-easyinput class="input-value" v-model="valueInfo.value" style="font-size: 16px;"
					placeholder="请输入提现金额" placeholderStyle="font-size:13px" clearSize="18" :inputBorder="false" />
				<view class="all" @click="valueInfo.value=balanceInfo.surplus || 0">
					全部提现
				</view>
			</view>
			<view class="balance">
				可用余额 ¥{{balanceInfo.surplus || 0}}
			</view>
		</view>
		<view class="tip">
			<text>*</text>
			提现手续费需要5%税费，20%积分，100的倍数起提
		</view>
		<view>
			<button size="default" type="default" class="submit" hover-class="none" @click="submit">提交</button>
		</view>
	</view>
	<uni-popup ref="verifyDialog" type="dialog" :animation="false">
		<uni-popup-dialog ref="inputClose" mode="input" title="短信验证码" placeholder="请输入短信验证码"
			@confirm="dialogInputConfirm"></uni-popup-dialog>
	</uni-popup>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';
	import assetApi from '@/sheep/api/member/asset'
	import bankAcountApi from '@/sheep/api/member/bankAcount';
	import {
		onLoad
	} from '@dcloudio/uni-app';

	import sheep from '@/sheep';
	const verifyDialog = ref(null)
	const valueInfo = reactive({
		value: undefined,
		type: 'SURPLUS',
		pyeeName: ''
	})
	const onClickNavBack = () => {
		uni.switchTab({
			url: '/pages/index/me'
		})
	}
	const chooseCard = () => {
		uni.navigateTo({
			url: '/pages/me/bankCard?from=SURPLUS'
		})
		uni.setStorageSync('pyeeName', valueInfo.pyeeName);
	}

	const balanceInfo = ref({})
	//获取余额
	const getBalance = async () => {
		let res = await assetApi.getAssetDetails('SURPLUS')
		console.log('获取余额', res)
		// setTimeout(() => {
		if (res.code == 0) {
			balanceInfo.value = res.data
		}

	}
	let secondVerifyInfo = {
		userId: uni.getStorageSync('userId') || '',
		token: '',
		verifyCode: '',
		totalAmount: '',
		txnSeqno: ''
	}
	const dialogInputConfirm = async (e) => {
		console.log('e', e);
		secondVerifyInfo.verifyCode = e;
		secondVerifyInfo.totalAmount = Number(valueInfo.value).toFixed(2);
		console.log('secondVerifyInfo', secondVerifyInfo)
		let res = await assetApi.secondaryVerification(secondVerifyInfo)
		console.log('提现短信验证', res)
		if (res.data.ret_code == '8889') {
			// sheep.$helper.toast('提交成功');
			sheep.$helper.toast(res.data.ret_msg);
		} else {
			sheep.$helper.toast(res.data.ret_msg);
		}
	}
	// 银行卡号格式验证函数
	function isValidCardNumber(cardNumber) {
		// 这里只是一个简单的示例，实际应用中应该根据具体需求调整
		// 比如银行卡号的长度、前缀等规则
		return /^\d{16,19}$/.test(cardNumber); // 假设银行卡号长度在16到19位之间
	}

	//确认提现
	const submit = async () => {

		// if (!valueInfo.pyeeName) return sheep.$helper.toast('请输入收款人姓名');
		// uni.setStorageSync('pyeeName', valueInfo.pyeeName);
		// if (!cardNo.value) return sheep.$helper.toast('请选择到账银行卡');
		// 验证银行卡号
		if (!cardNo.value) return sheep.$helper.toast('请输入到账银行卡号');

		// 验证银行卡号格式
		if (!isValidCardNumber(cardNo.value)) return sheep.$helper.toast('银行卡号格式不正确');
		if (!valueInfo.value || valueInfo.value == 0) return sheep.$helper.toast('请输入提现金额');

		if (uni.getStorageSync('userId') != "1817840189498839042") {
			if (valueInfo.value % 100 != 0) return sheep.$helper.toast('提现金额必须为100的倍数');
		}

		if (valueInfo.value > balanceInfo.value.surplus) return sheep.$helper.toast('提现金额不能大于可用余额');


		console.log('确认提现', valueInfo)
		let obj = {
			// type: valueInfo.type,
			value: valueInfo.value
		}
		let res = await assetApi.withDrawal(obj)
		console.log('提现', res)
		if (res.data) {
			sheep.$helper.toast('提现成功', 1000)
			setTimeout(() => {
				getBalance()
			}, 1500)
		}
		// //获取密码因子
		// let data = {
		// 	userId: uni.getStorageSync('userId') || ''
		// }
		// let res = await bankAcountApi.getAcctmgrRandom(data)
		// console.log('获取密码因子', res);

		// //获取密码控件token
		// let resp = await bankAcountApi.getAcctmgrPasswordToken({
		// 	userId: uni.getStorageSync('userId') || '',
		// 	amount: Number(valueInfo.value).toFixed(2),
		// 	pyeeName: valueInfo.pyeeName,
		// 	passwordScene: 'cashout_password'
		// })
		// console.log('获取密码控件token', resp);

		// if (res.data.ret_code == "0000") {
		// 	// 以下参数通过服务端获取
		// 	lianlianpay.invokePasswordControl({
		// 		/**
		// 		 * passwordScene 使用场景
		// 		 * setting_password-设置密码
		// 		 * change_password-修改密码
		// 		 * bind_card_password-换绑卡
		// 		 * cashout_password-提现
		// 		 * pay_password-支付
		// 		 */
		// 		passwordScene: 'cashout_password',
		// 		oidPartner: res.data.oid_partner, // 商户号
		// 		userId: res.data.user_id, // 用户号
		// 		passwordElementToken: resp.data, // 向服务端请求生成的 token
		// 		env: 'production', // 默认 production-生产
		// 		styles: { // 样式集合
		// 			themeColor: '#0E59F0', // 主题颜色（按钮等）
		// 		}
		// 	}, async (passwordObj) => {
		// 		console.log('密码数据', passwordObj)
		// 		if (passwordObj.code == '0000') {



		// 			let data = {
		// 				value: Number(valueInfo.value),
		// 				type: 'SURPLUS',
		// 				linkedAgrtNo: cardNo.value,
		// 				randomKey: passwordObj.data.randomKey,
		// 				password: passwordObj.data.password,
		// 			}

		// 			let res = await assetApi.withDrawal(data)
		// 			console.log('提现', res)
		// 			if (res.data.ret_code == '8888') {
		// 				secondVerifyInfo.token = res.data.token;
		// 				secondVerifyInfo.txnSeqno = res.data.txn_seqno

		// 				sheep.$helper.toast(res.data.ret_msg, 1000);
		// 				setTimeout(() => {
		// 					getBalance()
		// 				}, 1500)
		// 				verifyDialog.value.open()
		// 			}
		// 		}

		// 	})

		// }

		// let res = await assetApi.withDrawal(valueInfo)
		// console.log('提现', res)
		// if (res.code == 0) {
		// 	sheep.$helper.toast(res.data);
		// 	setTimeout(() => {
		// 		getBalance()
		// 	}, 3000)

		// }

	}


	const cardNo = ref('')

	onLoad((ops) => {
		console.log('ops', ops.ops)
		cardNo.value = ops.cardNo
		getBalance()
		valueInfo.pyeeName = uni.getStorageSync('pyeeName') || '';
	})
</script>

<style lang="scss" scoped>
	.nav-title {
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-items: center;
		justify-content: center;
		flex: 1;
	}

	.line {
		height: 1px;
		width: 100%;
		margin-bottom: 30px;
		background-color: #e5e2e2;
	}

	.value-box {
		margin: 20px 10px 10px 10px;
		padding: 10px 15px;
		box-sizing: border-box;
		background-color: #fff;
		border-radius: 10px;
		box-shadow: 5px 5px 10px #aaaaaa,
			-5px -5px 10px #ffffff;

		.title {
			font-size: 14px;
		}

		.value {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20px 0;

			.left {
				display: flex;
				align-items: center;

				.cardNo {
					font-size: 14px;
					max-width: 90%;
					white-space: nowrap;
					/* 禁止换行 */
					overflow: hidden;
					/* 超出部分隐藏 */
					text-overflow: ellipsis;
					/* 显示省略号 */
				}
			}
		}

		.icon {
			width: 25px;
			height: 15px;
			margin-right: 5px;
		}
	}

	.value-box {

		// margin: 10px;
		// padding: 10px 15px;
		// box-sizing: border-box;
		// background-color: #fff;
		// border-radius: 10px;
		// box-shadow: 5px 5px 10px #aaaaaa,
		// 	-5px -5px 10px #ffffff;

		.title {
			font-size: 14px;
		}

		.value {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 20px 0;

			.rmb {
				font-size: 20px;
				font-weight: bold;
			}

			.input-value {
				padding: 0 10px;
				font-size: 16px;
			}

			.all {
				font-size: 12px;
				color: #4176ED;
			}
		}

		.balance {
			font-size: 11px;
			color: #999;
		}
	}

	.tip {
		font-size: 12px;
		color: #999;
		margin: 15px 0 0 15px;

		text {
			color: red;
		}
	}

	.submit {
		margin-top: 30px;

		font-size: 16px;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 40px;
		color: #ffffff;
		background: #457CF0;
		width: 80%;
		border-radius: 50px;
		box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.3), -4px -4px 8px rgba(255, 255, 255, 0.2)
	}
</style>